package com.app.signing.widget;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;
import com.app.signing.R;
import com.app.signing.data.SpManager;
import com.app.signing.view.login.LoginStates;
import java.util.Objects;

public class LoginStatusDialog extends SigningSettingDialog {

    private RadioGroup radioGroup;

    public static LoginStatusDialog newInstance() {
        return new LoginStatusDialog();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
        builder.setView(buildCustomView())
                .setTitle(createTitle())
                .setPositiveButton(R.string.determine, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        int checkId = radioGroup.getCheckedRadioButtonId();
                        int statusId = LoginStates.loginDefault;
                        if (checkId == R.id.rb_states_default) {
                            statusId = LoginStates.loginDefault;
                        } else if (checkId == R.id.rb_states_special) {
                            statusId = LoginStates.loginSpecial;
                        }
                        SpManager.Companion.getINSTANCE().saveLoginStates(statusId);
                        dialogListener.onDialogPositiveClick(LoginStatusDialog.this,
                                String.valueOf(statusId));
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialogListener.onDialogNegativeClick(LoginStatusDialog.this);
                    }
                });
        return builder.create();
    }

    @Override
    protected String createTitle() {
        return "选择座位模式";
    }

    @Override
    protected View buildCustomView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.login_status_dialog, null);
        radioGroup = view.findViewById(R.id.rg_login_status);
        return view;
    }
}
